//
//  PersonCentreViewController.m
//  Fangmm
//
//  Created by ChaoZhou on 16/3/4.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "PersonCentreViewController.h"
#import "TwoButtonsPopViewController.h"
#import "FilterSortByViewController.h"
#import "SingleTablePopViewController.h"
#import "EditSingleTextItemViewController.h"
#import "NewMobileVerifyViewController.h"
#import "IdCardViewController.h"
#import "CompanyPositionViewController.h"
#import "ChangeMobileViewController.h"
#import "PersonTagPopViewController.h"
#import "PersonAccountViewController.h"
#import "UserHomepageViewController.h"
#import "MainNavigationViewController.h"

#import "HeadTableViewCell.h"
#import "NormalTableViewCell.h"
#import "IDCardTableViewCell.h"
#import "TagTableViewCell.h"

#import <SDWebImage/UIImageView+WebCache.h>
#import <SDWebImage/UIButton+WebCache.h>
#import <MobClick.h>
#import "ACMacros.h"
#import "Utility.h"

#import "HttpServer.h"
#import "User.h"
#import "Constants.h"

@interface PersonCentreViewController() <UITableViewDataSource,UITableViewDelegate, FilterSortByViewDelegate>

@end

@implementation PersonCentreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureNavigationBar];
    
    [self.mainTableView registerNib:[UINib nibWithNibName:@"HeadTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"headTableViewCell"];
    [self.mainTableView registerNib:[UINib nibWithNibName:@"IDCardTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"iDCardTableViewCell"];
    [self.mainTableView registerNib:[UINib nibWithNibName:@"TagTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"tagTableViewCell"];
    [self.mainTableView registerNib:[UINib nibWithNibName:@"NormalTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"normalTableViewCell"];
}

- (void)viewWillAppear:(BOOL)animated {
    if (self.mainTableView != nil) {
        [self.mainTableView reloadData];
        [self putUserInfos];
    }
    [MobClick beginLogPageView:@"个人中心页"];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [MobClick endLogPageView:@"个人中心页"];
}
- (void)configureNavigationBar {
    self.title = @"个人中心";
    self.navigationItem.hidesBackButton = true;
//    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc]initWithTitle:@"我的主页" style:UIBarButtonItemStyleBordered target:self action:@selector(jumpToMyHomePage)];
//    [self.navigationItem setLeftBarButtonItem:leftBar];
//    [self.navigationItem setRightBarButtonItem:rightBar];
    
    UIImage *backImage = [[UIImage imageNamed:@"back-18-36"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithImage:backImage style:UIBarButtonItemStyleDone target:self action:@selector(jumpBack)];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithTitle:@"我的主页" style:UIBarButtonItemStyleDone target:self action:@selector(jumpToMyHomePage)];
    [rightBarButton setTintColor:RGBACOLOR(119, 119, 119, 1)];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    self.navigationItem.rightBarButtonItem = rightBarButton;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section)
    {
        case 0:
        {
            if (indexPath.row == 0)
            {
                return 90;
            }
            return 50;
        }
            break;
        case 1:
        {
            return 50;
        }
            break;
        case 2:
        {
            switch (indexPath.row)
            {
                case 0:
                    return 50;
                    break;
                default:
                    return 70;
                    break;
            }
        }
            break;
        default:
        {
            return 50;
        }
            break;
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section)
    {
        case 0:
            return 5;
            break;
        case 1:
            return 3;
            break;
        case 2:
            return 2;
            break;
        case 3:
            return 1;
            break;
        default:
            return 0;
            break;
    }
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section)
    {
        case 0:
        {
            switch (indexPath.row)
            {
                case 0:
                {
                    HeadTableViewCell *headerTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"headTableViewCell"];
                    User *user = [User sharedUser];
                    [headerTableViewCell.headerImage sd_setImageWithURL:[NSURL URLWithString:user.avatar]];
                    headerTableViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return headerTableViewCell;
                }
                    break;
                case 1:
                {
                    NormalTableViewCell *nickNameTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"normalTableViewCell"];
                    [nickNameTableViewCell.titleLabel setText:@"昵称"];
                    [nickNameTableViewCell.contentLabel setText:[User sharedUser].name];
                    nickNameTableViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return nickNameTableViewCell;
                }
                    break;
                case 2:
                {
                    NormalTableViewCell *sexTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"normalTableViewCell"];
                    [sexTableViewCell.titleLabel setText:@"性别"];
                    if ([[User sharedUser].gender isEqual:@"f"]) {
                        [sexTableViewCell.contentLabel setText:@"女"];
                    } else {
                        [sexTableViewCell.contentLabel setText:@"男"];
                    }
                    sexTableViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return sexTableViewCell;
                }
                    break;
                case 3:
                {
                    NormalTableViewCell *yearTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"normalTableViewCell"];
                    [yearTableViewCell.titleLabel setText:@"年龄"];
                    [yearTableViewCell.contentLabel setText:[NSString stringWithFormat:@"%d后",[User sharedUser].ageGroup]];
                    yearTableViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return yearTableViewCell;
                }
                    break;
                case 4:
                {
                    NormalTableViewCell *mobileTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"normalTableViewCell"];
                    [mobileTableViewCell.titleLabel setText:@"手机号"];
                    [mobileTableViewCell.contentLabel setText:[User sharedUser].mobile];
                    mobileTableViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return mobileTableViewCell;
                }
                    break;
                default:
                    return nil;
                    break;
            }
        }
            break;
        case 1:
        {
            switch (indexPath.row)
            {
                case 0:
                {
                    IDCardTableViewCell *verifyIDCardTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"iDCardTableViewCell"];
                    if ([User sharedUser].certification) {
                        [verifyIDCardTableViewCell.verifyIDLabel setText:@"已认证身份证"];
                        [verifyIDCardTableViewCell.IDcardImage setImage:[UIImage imageNamed:@"check_ID-44-36"]];
                    } else {
                        [verifyIDCardTableViewCell.verifyIDLabel setText:@"未认证身份证"];
                        [verifyIDCardTableViewCell.IDcardImage setImage:[UIImage imageNamed:@"check_ID_dim-44-36"]];
                    }
                    verifyIDCardTableViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return verifyIDCardTableViewCell;
                }
                    break;
                case 1:
                {
                    NormalTableViewCell *industryTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"normalTableViewCell"];
                    [industryTableViewCell.titleLabel setText:@"行业"];
                    [industryTableViewCell.contentLabel setText:[User sharedUser].industry];
                    industryTableViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return industryTableViewCell;

                }
                    break;
                case 2:
                {
                    NormalTableViewCell *jobTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"normalTableViewCell"];
                    [jobTableViewCell.titleLabel setText:@"公司 - 职业"];
                    [jobTableViewCell.contentLabel setText:[User sharedUser].profession];
                    jobTableViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return jobTableViewCell;
                }
                    break;
                default:
                    break;
            }
        }
            break;
        case 2:
        {
            switch (indexPath.row)
            {
                case 0:
                {
                    NormalTableViewCell *constellationTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"normalTableViewCell"];
                    [constellationTableViewCell.titleLabel setText:@"星座"];
                    [constellationTableViewCell.contentLabel setText:[User sharedUser].constellation];
                    constellationTableViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return constellationTableViewCell;
                }
                    break;
                case 1:
                {
                    TagTableViewCell *tagLabelTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"tagTableViewCell"];
                    NSArray *tags = [User sharedUser].tags;
                    NSArray *labels = @[tagLabelTableViewCell.tag_1_Label, tagLabelTableViewCell.tag_2_Label, tagLabelTableViewCell.tag_3_Label, tagLabelTableViewCell.tag_4_Label, tagLabelTableViewCell.tag_5_Label, tagLabelTableViewCell.tag_6_Label];
                    for (UILabel *label in labels) {
                        [label setHidden:true];
                        [label.layer setBorderColor:[RGBACOLOR(255, 118, 108, 1) CGColor]];
                        [label.layer setBorderWidth:0.8];
                    }
                    for (int i = 0 ; i < tags.count; i++) {
                        if (tags.count <= labels.count) {
                            [labels[i] setHidden:false];
                            UILabel *label = labels[i];
                            [label setText:tags[i]];
                        }
                    }

                    tagLabelTableViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return tagLabelTableViewCell;
                }
                    break;
                default:
                    break;
            }
            
        }
            break;
        case 3:
        {
            NormalTableViewCell *accountTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"normalTableViewCell"];
            [accountTableViewCell.titleLabel setText:@"账户余额"];
            int balance = [[Utility getObjectForKey:kBalance] intValue];
            int trustFee = [[Utility getObjectForKey:kTrusteeFee] intValue];
            [accountTableViewCell.contentLabel setText:[NSString stringWithFormat:@"¥%d",balance - trustFee]];
            accountTableViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
            return accountTableViewCell;
        }
            break;
        default:
            return nil;
            break;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section)
    {
        case 0:
        {
            switch (indexPath.row)
            {
                case 0:
                {
                    TwoButtonsPopViewController *editHeaderVC = [[TwoButtonsPopViewController alloc]initWithNibName:@"TwoButtonsPopViewController" bundle:nil];
                    [self presentViewController:editHeaderVC animated:true completion:^{
                        editHeaderVC.sourceView = @"Avatar";
                        [editHeaderVC.titleLabel setText:@"编辑头像"];
                        [editHeaderVC.firstButton setBackgroundImage:[UIImage imageNamed:@"home_camera"] forState:UIControlStateNormal];
                        [editHeaderVC.firstLabel setText:@"拍照"];
                        [editHeaderVC.secondButton setBackgroundImage:[UIImage imageNamed:@"home_photo"] forState:UIControlStateNormal];
                        [editHeaderVC.secondLabel setText:@"相册选取"];
                    }];
                }
                    break;
                case 1:
                {
                    EditSingleTextItemViewController *editNickVC = [[EditSingleTextItemViewController alloc]init];
                    [self.navigationController pushViewController:editNickVC animated:YES];
                }
                    break;
                case 2:
                {
                    TwoButtonsPopViewController *sexChooseVC = [[TwoButtonsPopViewController alloc] init];
                    [self presentViewController:sexChooseVC animated:true completion:^{
                        sexChooseVC.sourceView = @"Gender";
                        [sexChooseVC.titleLabel setText:@"编辑头像"];
                        [sexChooseVC.firstButton setBackgroundImage:[UIImage imageNamed:@"home_boy"] forState:UIControlStateNormal];
                        [sexChooseVC.firstLabel setText:@"男"];
                        [sexChooseVC.secondButton setBackgroundImage:[UIImage imageNamed:@"home_girl"] forState:UIControlStateNormal];
                        [sexChooseVC.secondLabel setText:@"女"];
                    }];
                }
                    break;
                case 3:
                {
                    FilterSortByViewController *yearOldSequenceVC = [[FilterSortByViewController alloc]init];
                    yearOldSequenceVC.delegate = self;
                    yearOldSequenceVC.sourceDataArray = @[@"80后", @"85后", @"90后", @"95后"];
                    if ([User sharedUser].ageGroup == 80) {
                        yearOldSequenceVC.selectRow = 0;
                    } else if ([User sharedUser].ageGroup == 85) {
                        yearOldSequenceVC.selectRow = 1;
                    } else if ([User sharedUser].ageGroup == 90) {
                        yearOldSequenceVC.selectRow = 2;
                    } else if ([User sharedUser].ageGroup == 95) {
                        yearOldSequenceVC.selectRow = 3;
                    }
                    [self presentViewController:yearOldSequenceVC animated:true completion:^{
                    }];
                }
                    break;
                default:
                {
                    ChangeMobileViewController *mobileVC = [[ChangeMobileViewController alloc]init];
                    [self.navigationController pushViewController:mobileVC animated:YES];
                }
                    break;
            }
        }
            break;
        case 1:
        {
            switch (indexPath.row)
            {
                case 0:
                {
                    IdCardViewController *idCardVC = [[IdCardViewController alloc]init];
                    [self.navigationController pushViewController:idCardVC animated:YES];
                }
                    break;
                case 1:
                {
                    SingleTablePopViewController *industryVC = [[SingleTablePopViewController alloc]initWithNibName:@"SingleTablePopViewController" bundle:nil];
                    industryVC.popType = @"行业";
                    industryVC.selectedItem = [User sharedUser].industry;
                    [self presentViewController:industryVC animated:true completion:^{
                    }];
                }
                    break;
                default:
                {
                    CompanyPositionViewController *jobVC = [[CompanyPositionViewController alloc]init];
                    
                    [self.navigationController pushViewController:jobVC animated:YES];
                }
                    break;
            }
        }
            break;
        case 2:
        {
            switch (indexPath.row)
            {
                case 0:
                {
                    SingleTablePopViewController *constellationVC =[[SingleTablePopViewController alloc]init];
                    constellationVC.popType = @"星座";
                    constellationVC.selectedItem = [User sharedUser].constellation;
                    [self presentViewController:constellationVC animated:true completion:^{
                    }];
                }
                    break;
                default:
                {
                    PersonTagPopViewController *personTagVC = [[PersonTagPopViewController alloc]initWithNibName:@"PersonTagPopViewController" bundle:nil];
                    [self presentViewController:personTagVC animated:true completion:^{
                    }];
                }
                    break;
            }
        }
            break;
        default:
        {
            PersonAccountViewController *accountVC = [[PersonAccountViewController alloc]init];
            [self.navigationController pushViewController:accountVC animated:YES];
        }
            break;
    }
}

-(void)jumpBack
{
    if ([[self.navigationController class] isSubclassOfClass:[MainNavigationViewController class]]) {
        [self.navigationController popToRootViewControllerAnimated:true];
    } else {
        [self.navigationController popViewControllerAnimated:true];
    }
}
-(void)jumpToMyHomePage
{
    UserHomepageViewController *userHomepageVC = [[UserHomepageViewController alloc]init];
    userHomepageVC.userId = [User sharedUser].userId;
    userHomepageVC.userName = [User sharedUser].name;
    [self.navigationController pushViewController:userHomepageVC animated:true];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - Network methods
- (void)putUserInfos {
    [HttpServer putUserInfoWithSuccessBlock:^(NSDictionary *successDic) {
        [[User sharedUser] setValuesForKeysWithDictionary:[successDic objectForKey:@"user"]];
        [Utility setObject:[User sharedUser] key:kUser];
    } withFailBlock:^(id failData) {
        NSLog(@"%@",failData);
    }];
}

- (void)getBalance {
    [HttpServer getBalanceWithSuccessBlock:^(NSDictionary *successDic) {
//        NormalTableViewCell *accountTable = s
    } withFailedBlock:^(id failData) {
        NSLog(@"%@",failData);
    }];
}

- (void)didSelectedRow:(NSInteger)row WithItem:(id)item {
    User *user = [User sharedUser];
        switch (row) {
            case 0:
                user.ageGroup = 80;
                break;
            case 1:
                user.ageGroup = 85;
                break;
            case 2:
                user.ageGroup = 90;
                break;
            case 3:
                user.ageGroup = 95;
                break;
    
            default:
                break;
        }
}

@end
